import { useSelector, useDispatch } from "react-redux"
import { useCallback } from "react";
import AddressApi from "../api/address.js"
import UsersApi from "../api/users.js"
import Creater from "../store/actions"


export const useAmendAddress = () => {
    // 获取dispatch方法，需要使用其触发reducers 
    const dispatch = useDispatch()
    //获取状态机内address的状态值
    const amendAddress = useSelector(state => state.amendAddress)

    //相当于先把要修改的数据保存至状态机
    const setAmend = useCallback((data) => { dispatch(Creater.amendAddress(data)) });

    //  发送请求newAddress是修改调用amend_address方法传的参数 vlues 修改自提点数据
    const amend_address = useCallback(async (newAddress) => {
        const data = await AddressApi.amendAddress(newAddress)

        return data
    });

    //删除 图片
    const delImag = async (tableItem) => {
        const { _id, Image } = tableItem

        const token = UsersApi.getToken(); //获取token

        const {data} = await AddressApi.deleteImag({ _id, Image, token });
        dispatch(Creater.amendAddress(data))
       
    }
    return [amendAddress, { setAmend, amend_address, delImag }]
}

